package com.bsh.utils.system.smsApi;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.bsh.utils.StringUtils;
import com.bsh.utils.system.SmsReponseMessage;

/**
 * 短信提醒
 * 
 * @author gsh
 *
 */
@Component
@Lazy(value = false)
public class SmsNotice {
	private static Logger log = LoggerFactory.getLogger(SmsNotice.class);
	static String cashMobiles;  //要提醒的手机号 
	static String cashTpl;		//提现模板
	static String rechargeTpl;  //充值模板
	static String transferTpl;  //转帐模板
	@Autowired
	SmsApiYp sysApiYp;

	private static String decUtf8Tpl(String tpl) {
		String decTpl = null;
		try {
			decTpl = new String(tpl.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return decTpl;
	}
	
	
	/**
	 * 提现提醒
	 * @param money
	 */
	public void sendCashNotice(String cashMobile,String money){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dt=sdf.format(new Date());		
		String[] mobiles=getCashMobiles();		
		for (String mobile : mobiles) {
			String content = String.format(decUtf8Tpl(cashTpl),cashMobile,dt,money);
			SmsReponseMessage res = sysApiYp.sendMsg(mobile, content);
			if(res.getCode()!=0){
				log.error(String.format("发送提现提醒失败，消息：%s\r\n code:%s,原因：%s",content,res.getCode(),res.getMsg()));
			}
		} 
	}
	
	/**
	 * 充值提醒
	 * @param money
	 */
	public void sendRechargeNotice(String rechargeMobile,String money){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dt=sdf.format(new Date());		
		String[] mobiles=getCashMobiles();		
		for (String mobile : mobiles) {
			String content = String.format(decUtf8Tpl(rechargeTpl),rechargeMobile,dt,money);
			SmsReponseMessage res = sysApiYp.sendMsg(mobile, content);
			if(res.getCode()!=200){
				log.error(String.format("发送充值提醒失败，消息：%s\r\n code:%s,原因：%s",content,res.getCode(),res.getMsg()));
			}
		} 
	}
	
	/**
	 * 转帐提醒
	 * @param money
	 */
	public void sendTransferNotice(String fransferMobileSrc,String fransferMobileTarg,String money){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dt=sdf.format(new Date());		
		String[] mobiles=getCashMobiles();		
		for (String mobile : mobiles) {
			String content = String.format(decUtf8Tpl(transferTpl),fransferMobileSrc,fransferMobileTarg,dt,money);
			SmsReponseMessage res = sysApiYp.sendMsg(mobile, content);
			if(res.getCode()!=200){
				log.error(String.format("发送转帐提醒失败，消息：%s\r\n code:%s,原因：%s",content,res.getCode(),res.getMsg()));
			}			
		} 
	}
	
	
	public static String[] getCashMobiles() {
		if(!org.apache.commons.lang3.StringUtils.isNotEmpty(cashMobiles))return null;
		return cashMobiles.split(",");
	}

	@Value("${sms.notice.cash_mobiles}")
	public void setCashMobiles(String cashMobiles) {
		SmsNotice.cashMobiles = cashMobiles;
	}

	public static String getCashTpl() {
		return cashTpl;
	}

	@Value("${sms.notice.cash_tpl}")
	public void setCashTpl(String cashTpl) {
		SmsNotice.cashTpl = cashTpl;
	}

	public static String getRechargeTpl() {
		return rechargeTpl;
	}

	@Value("${sms.notice.recharge_tpl}")
	public void setRechargeTpl(String rechargeTpl) {
		SmsNotice.rechargeTpl = rechargeTpl;
	}

	public static String getTransferTpl() {
		return transferTpl;
	}

	@Value("${sms.notice.transfer_tpl}")
	public void setTransferTpl(String transferTpl) {
		SmsNotice.transferTpl = transferTpl;
	}

	
}
